<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	$result['status'] = '0';
	
	$m =  getParam('m','get');
	//修改密码流程
	$oldphone =  getParam('oldphone','get');
	$oldcode =  getParam('oldcode','get');
	$newphone =  getParam('newphone','get');
	$newcode =  getParam('newcode','get');
	$valid =  getParam('valid','get');
	
	
	if($m == 'editpwd1')
	{
		if(empty($oldphone))
		{
			$result['msg'] = '请输入手机号码';
			echoJSON($result);
			exit;
		}
		
		if(empty($oldcode))
		{
			$result['msg'] = '请输入收到的验证码';
			echoJSON($result);
			exit;
		}
		
		unset($wheres);
	
		$wheres['loginname'] = $oldphone;
		
		$info = _SqlTool::getInfo('users', $wheres);
		
		if(empty($info))
		{
			$result['msg'] = '未查询到手机信息';
			echoJSON($result);
			exit;
		}
		
		unset($where);
		
		$where['phone'] = $oldphone;
		$where['today'] = strtotime(date('Y-m-d'));
		if($uservalid = _User::getUserValidCode($where))
		{
			if($uservalid['errornums'] >= 5)
			{
				$result['msg'] = '验证码错误次数过多';
				echoJSON($result);
				exit;
			}
			
			if($uservalid['code'] != $oldcode)
			{
				unset($query,$data);
				$query['id'] = $uservalid['id'];
				$data['errornums'] = $uservalid['errornums']+1;
				
				_User::setUserValidCode($query, $data);
				$result['msg'] = '验证码输入错误';
				echoJSON($result);
				exit;
			}
			else
			{
				//删除验证码
				unset($query);
				$query['phone'] = $oldphone;
				_User::delUserValidCode($query);
				
				$result['status'] = '1';
				$result['msg'] = '验证通过';
				$result['valid'] = md5($oldphone.'fghr@#$%#$#');
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['msg'] = '请输入收到的验证码';
			echoJSON($result);
			exit;
		}
	}
	
	if($m == 'editpwd2')
	{
		if(empty($oldphone))
		{
			$result['msg'] = '缺少原手机手机号码';
			echoJSON($result);
			exit;
		}
		
		if($valid != md5($oldphone.'fghr@#$%#$#'))
		{
			$result['msg'] = '验证错误';
			echoJSON($result);
			exit;
		}
		
		if(empty($newphone))
		{
			$result['msg'] = '缺少更换的手机号码';
			echoJSON($result);
			exit;
		}
		
		
		if($newphone == $oldphone)
		{
			$result['msg'] = '两个手机号不能一样';
			echoJSON($result);
			exit;
		}
		
		if(empty($newcode))
		{
			$result['msg'] = '请输入更换的手机号码收到的验证码';
			echoJSON($result);
			exit;
		}
		
		unset($wheres);
	
		$wheres['loginname'] = $newphone;
		
		$info = _SqlTool::getInfo('users', $wheres);
		
		if(!empty($info))
		{
			$result['msg'] = '更换的手机号已经注册';
			echoJSON($result);
			exit;
		}
		
		unset($where);
	
		$where['phone'] = $newphone;
		$where['today'] = strtotime(date('Y-m-d'));
		if($uservalid = _User::getUserValidCode($where))
		{
			if($uservalid['errornums'] >= 5)
			{
				$result['msg'] = '验证码错误次数过多';
				echoJSON($result);
				exit;
			}
		
			if($uservalid['code'] != $newcode)
			{
				unset($query,$data);
				$query['id'] = $uservalid['id'];
				$data['errornums'] = $uservalid['errornums']+1;
				
				_User::setUserValidCode($query, $data);
				$result['msg'] = '验证码输入错误';
				echoJSON($result);
				exit;
			}
			
			//删除验证码
			unset($query);
			$query['phone'] = $newphone;
			_User::delUserValidCode($query);
		}
		else
		{
			$result['msg'] = '请输入更换的手机号码收到的验证码';
			echoJSON($result);
			exit;
		}
		
		unset($where);
	
		$wheres['loginname'] = $oldphone;
		
		$info = _SqlTool::getInfo('users', $wheres);
		
		if(!empty($info['id']))
		{
			unset($where,$data);
			$where['id'] = $info['id'];
			$data['loginname'] = $newphone;
			$res = _SqlTool::setInfo('users', $where, $data);
			
			if(!empty($res))
			{
				$result['msg'] = '变更手机号成功';
				$result['status'] = '1';
			}
			else
			{
				$result['msg'] = '变更手机号失败';
			}
		}
		else
		{
			$result['msg'] = '变更手机号失败';
		}
		
	}
	
	echoJSON($result);